package com.liveneo.adapter.mobile.web.controller;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.entity.PrpmLogVideo;
import com.liveneo.adapter.base.entity.PrpmLoggerInfo;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmVideoUser;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.dto.LossItemDto;
import com.liveneo.adapter.mobile.model.request.LogRecordRequest;
import com.liveneo.adapter.mobile.model.request.LogVideoRequest;
import com.liveneo.adapter.mobile.model.response.LogRecordResponse;
import com.liveneo.adapter.mobile.model.response.LogVideoResponse;
import com.liveneo.adapter.mobile.service.LoggerInfoService;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.mobile.service.PrpmLogVideoService;
import com.liveneo.adapter.mobile.service.PrpmSurveyUserService;
import com.liveneo.adapter.mobile.service.PrpmVideoUserService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * @Desc：日志记录接口
 * @Title：MobileLogRecordController
 * @address: http://www.liveneo.com.cn/
 * @author: zhaoyy
 * @version: 1.0 
 * @date: 2016年10月14日 下午2:28:32
 */
@Controller
@RequestMapping("/mobile")
public class MobileLogRecordController extends BaseController {
    @Autowired
    private LoggerInfoService     loggerInfoService;
    @Autowired
    private PrpmLogVideoService   LogVideoService;
    @Autowired
    private LossItemService       lossItemService;
    @Autowired
    private PrpmVideoUserService  videoUserService;
    @Autowired
    private PrpmSurveyUserService surveyUserService;

    /**
     * MP59
     * @Desc: 记录异常日志
     * @Title: 日志记录接口
     * @Author: zhaoyy 2016年10月14日 下午2:46:49
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping("/logRecord.action")
    public String logRecord(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP59入参=" + loguuid;
        LogRecordRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LogRecordRequest.class);
        LogRecordResponse response = null;
        String result = "";
        try {
            PrpmLoggerInfo prpmloggerinfo = new PrpmLoggerInfo();
            prpmloggerinfo.setUuid(UUIDUtils.getUUIDStr());
            prpmloggerinfo.setMobile(request.getMobile());
            prpmloggerinfo.setCaseNo(request.getCaseNo());
            prpmloggerinfo.setLogMsg(request.getLogMsg());
            prpmloggerinfo.setLogTime(request.getLogTime());
            loggerInfoService.addLoggerInfo(prpmloggerinfo);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    LogRecordResponse.class);
        } catch (Exception e) {
            LogUtils.error("MP59日志记录接口==>错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LogRecordResponse.class);
        } finally {
            result = decorateResult("MP59出参=" + loguuid, response);
        }
        return result;
    }

    /**
     * MP70
     * @Desc: 视频连线业务日志记录
     * @Author: zhaoyy 2016年11月21日 下午2:22:32
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping("/insertLogVideo.action")
    public String insertLogVideo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP70入参=" + loguuid;
        LogVideoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, LogVideoRequest.class);
        LogVideoResponse response = null;
        String result = "";
        String caseNo = request.getCaseNo();
        try {
            if (StringUtils.isBlank(caseNo) || StringUtils.isEmpty(caseNo)) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.CODE_PARAM_NO_CASENO, LogVideoResponse.class);
            } else if (request.getLossItemList() == null || request.getLossItemList().size() == 0) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.CODE_PARAM_NO_LOSSUUID, LogVideoResponse.class);
            } else {
                List<LossItemDto> list = request.getLossItemList();
                for (LossItemDto item : list) {
                    PrpmLogVideo logVideo = new PrpmLogVideo();
                    logVideo.setLogVideoId(UUIDUtils.getUUIDStr());
                    logVideo.setCaseNo(caseNo);
                    PrpmLossItem lossItem = lossItemService.getLossItemByCaseNoLossIdUuid(caseNo, item.getLossId(), item.getLossUuid());
                    if (lossItem != null) {
                        logVideo.setLossUuid(lossItem.getLossItemId());
                        logVideo.setLossName(lossItem.getLossName());
                        logVideo.setLossType(lossItem.getLossType());
                        logVideo.setCheckLossUsercode(lossItem.getCheckLossUsercode());
                    }
                    logVideo.setTerminalId(request.getDeviceUuid());
                    PrpmVideoUser videoUser = videoUserService.selectByUserCode(request.getUserId());
                    if (videoUser != null) {
                        logVideo.setVcId(videoUser.getVcId());
                        logVideo.setGroupId(videoUser.getSkillId());
                        logVideo.setJobId(videoUser.getAgtId());
                    }
                    logVideo.setSurveyorCode(request.getUserId());
                    logVideo.setPhoneModel(request.getPhoneModel());
                    logVideo.setSysVer(request.getSystemInfo());
                    logVideo.setLogContent(request.getContent());
                    logVideo.setPlatType("2");
                    logVideo.setCreateTime(new Date());
                    if ("B".equals(request.getProjectCode())) {
                        PrpmSurveyUser surveyUser = surveyUserService.findUserByUserCode(request.getUserId());
                        if (surveyUser != null) {
                            logVideo.setCreateBy(surveyUser.getSurveyName());
                        }
                    } else {
                        logVideo.setCreateBy(request.getUserId());
                    }
                    LogVideoService.addLogVideo(logVideo);
                }
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        LogVideoResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("MP70日志记录接口==>错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    LogVideoResponse.class);
        } finally {
            result = decorateResult("MP70出参=" + loguuid, response);
        }
        return result;
    }
}
